package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntApplyMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntApplyMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntApplyInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.realize.PublicDealService;
import cc.rengu.igas.bsps.core.realize.impl.PublicDealServiceImpl;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.request.MchntExpandAcceptQueryRequest;
import cc.rengu.igas.bsps.facade.request.MchntExpandAcceptQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/10/29
 */
public class MchntExpandAcceptQueryService extends BankManagerService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntExpandAcceptQueryRequest mchntExpandAcceptQueryRequest = new MchntExpandAcceptQueryRequest();
        ConvertUtil.convertOutput(mchntExpandAcceptQueryRequest);
        return mchntExpandAcceptQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntExpandAcceptQueryRequest mchntExpandAcceptQueryRequest = (MchntExpandAcceptQueryRequest) request;
        MchntExpandAcceptQueryResponse mchntExpandAcceptQueryResponse = new MchntExpandAcceptQueryResponse();
        /*获取上送报文参数信息：法人行号申请单号*/
        String instId = mchntExpandAcceptQueryRequest.getHeader().getInstId();
        String userId = mchntExpandAcceptQueryRequest.getUserId();
        String applyId = mchntExpandAcceptQueryRequest.getApplyId();

        /*必填校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(applyId) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("请求参数校验必填字段失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        BspsMchntApplyMapper bspsMchntApplyMapper = new BspsMchntApplyMapperImpl();
        MchntApplyInfo mchntApplyInfo = bspsMchntApplyMapper.selectBspsMchntApplyInfoByPrimaryKey(instId, applyId);

        if (null == mchntApplyInfo) {
            rglog.error("商户申请信息查询失败！");
            throw new BizException(RespCodeEnum.GET_APPLY_INFO_ERROR.getRespCode(), RespCodeEnum.GET_APPLY_INFO_ERROR.getRespDesc());
        }

        if (!"00".equals(mchntApplyInfo.getApplyStatus())) {
            rglog.error("商户申请已入驻");
            throw new BizException(RespCodeEnum.MCHNT_APPLY_DONE_ERROR.getRespCode(), RespCodeEnum.MCHNT_APPLY_DONE_ERROR.getRespDesc());
        }
        BeanUtil.beanCopy(mchntApplyInfo, mchntExpandAcceptQueryResponse);
        PublicDealService publicDealService = new PublicDealServiceImpl();
        /**编译经营范围*/
        if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getManagement())) {
            mchntExpandAcceptQueryResponse.setManagementScope(mchntApplyInfo.getManagement());
        }

        /**地区编译*/
        if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getArea())) {
            mchntExpandAcceptQueryResponse.setAreaCode(mchntApplyInfo.getArea());
            mchntExpandAcceptQueryResponse.setArea(publicDealService.getRegionName(mchntApplyInfo.getArea()));
        }
        if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getCity())) {
            mchntExpandAcceptQueryResponse.setCity(publicDealService.getRegionName(mchntApplyInfo.getCity()));
        }
        if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getProvince())) {
            mchntExpandAcceptQueryResponse.setProvince(publicDealService.getRegionName(mchntApplyInfo.getProvince()));
        }

        /*拼装返回报文信息*/
        BizResponse<MchntExpandAcceptQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntExpandAcceptQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntExpandAcceptQueryResponse mchntExpandAcceptQueryResponse = (MchntExpandAcceptQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntExpandAcceptQueryResponse);
    }
}
